package com.example.deploysinggle.test1.common.handler;

import com.example.deploysinggle.test1.common.resp.CommonSingleResponse;
import com.example.deploysinggle.test1.common.util.ResponseUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.NoHandlerFoundException;

/**
 * @author Administrator
 * @version 1.0
 * @description: TODO
 * @date 2025/1/23 17:10
 */
@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {
    /**
     * @description:  处理404
     * @param:
     * @param ex mple
     * @return: com.exa.deploysinggle.test1.common.resp.CommonSingleResponse
     * @author Administrator
     * @date: 2025/1/24 9:49
     */
    @ResponseStatus(HttpStatus.NOT_FOUND)
    @ResponseBody
    @ExceptionHandler(NoHandlerFoundException.class)
    public CommonSingleResponse handleNotFound(NoHandlerFoundException ex) {
        log.error("404 Not Found:", ex);
        return ResponseUtil.allFail(ex);
    }

    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    @ResponseBody
    @ExceptionHandler(Exception.class)
    public CommonSingleResponse handleNotFound(Exception ex) {
        log.error("500 Exception:", ex);
        return ResponseUtil.allFail(ex);
    }

}
